﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

using ECCIT;
using ECCIT.Data;
using ECCIT.Web;
using OgilvyOne.Golf.Model;
using OgilvyOne.Golf.IDAL;

namespace OgilvyOne.Golf.DAL
{
    public class UserDAL : IDALuser
    {
        private const string C_TABLE_NAME = "golf_user_info";
        private const string C_COL_USER_ID = "ID";
        private const string C_COL_USER_UID="MYUID";
        private const string C_COL_USER_NAME="MYNAME";
        private const string C_COL_USER_PROIMAGE="MyProImage";
        private const string C_COL_USER_AVATAR="MyAvatar";
        private const string C_COL_INSERT_TIME="InsertTime";
        private const string C_COL_UPDATETIME="LastUpdateTime";

        private const string C_SQL_SELECT_ALL_USERS = "select " + C_COL_USER_ID + ","+ C_COL_USER_UID + "," + C_COL_USER_NAME + "," + C_COL_USER_PROIMAGE + "," + C_COL_USER_AVATAR + "," + C_COL_INSERT_TIME + "," + C_COL_UPDATETIME + " from " + C_TABLE_NAME;
        private const string C_SQL_INSERT_USER = "insert into " + C_TABLE_NAME + " ("
                             + C_COL_USER_UID + ","
                             + C_COL_USER_NAME + ","
                             + C_COL_USER_PROIMAGE + ","
                             + C_COL_USER_AVATAR + ","
                             + C_COL_INSERT_TIME + ","
                             +C_COL_UPDATETIME
                             +")values("
                             +"@"+C_COL_USER_UID+","
                             + "@" + C_COL_USER_NAME + ","
                             + "@" + C_COL_USER_PROIMAGE + ","
                             + "@" + C_COL_USER_AVATAR + ","
                             + "@" + C_COL_INSERT_TIME + ","
                             +"@"+C_COL_UPDATETIME
                             +"); select max("+C_COL_USER_ID+") from "
                             + C_TABLE_NAME
                             
                             ;

         const string selectbyID = "select * from"+C_TABLE_NAME+" where "+C_COL_USER_ID+"=@"+C_COL_USER_ID;
         const string selectbyUID = "select * from" + C_TABLE_NAME + " where " + C_COL_USER_UID + "=@" + C_COL_USER_UID;
         const string delbyUID = "delete "+C_TABLE_NAME
                                         +" where "
                                         +C_COL_USER_UID
                                    +"=@"+C_COL_USER_UID;
        
        public List<UserModel> GetAllWeiboUsers()
        {
            try
            {
                List<UserModel> users = new List<UserModel>();

                using (IDataReader reader = DBHelper.ExecuteReader(C_SQL_SELECT_ALL_USERS))
                {
                    while (reader.Read())
                    {
                        int id=(int)reader[C_COL_USER_ID];
                        string uid = (string)reader[C_COL_USER_UID];
                        string name = (string)reader[C_COL_USER_NAME];
                        string proiamge=(string)reader[C_COL_USER_PROIMAGE];
                        string avatar = (string)reader[C_COL_USER_AVATAR];
                        DateTime? insertTime=(DateTime)reader[C_COL_INSERT_TIME];
                        DateTime? upTime = (DateTime)reader[C_COL_UPDATETIME];
                        users.Add(new UserModel(id,uid,name,proiamge,avatar,insertTime,upTime));
                      
                    }
                }

                return users;
            }
            catch (Exception ex)
            {

                throw new Exception("Error occurred in UserDAL.GetAllWeiboUsers(). Message: " + ex.Message);
            }
        }

       private ResultInfo Save(UserModel modelInfo)
        {
            if (modelInfo == null) throw new ArgumentNullException("modelInfo");

            ResultInfo result = new ResultInfo(false);

            try
            {
                int rowsAffected = 0;
                if (modelInfo.MyID == null)   // insert
                {
                    //rowsAffected = Insert(modelInfo, null);
                    string cmdInsert = C_SQL_INSERT_USER ;
                    IDataParameter[] parms = new SqlParameter[6];
                    parms[0] = new SqlParameter(C_COL_USER_UID, SqlDbType.NVarChar, 50);
                    parms[1] = new SqlParameter(C_COL_USER_NAME, SqlDbType.NVarChar, 50);
                    parms[2] = new SqlParameter(C_COL_USER_PROIMAGE, SqlDbType.NVarChar,50);
                    parms[3] = new SqlParameter(C_COL_USER_AVATAR, SqlDbType.NVarChar,50);
                    parms[4] = new SqlParameter(C_COL_INSERT_TIME, SqlDbType.DateTime);
                    parms[5] = new SqlParameter(C_COL_UPDATETIME, SqlDbType.DateTime);
                    parms[0].Value = modelInfo.MyUID;
                    parms[1].Value = modelInfo.MyScreenName;
                    parms[2].Value = modelInfo.MyProImage;
                    parms[3].Value = modelInfo.MyAvatarImage;
                    parms[4].Value = DateTime.Now;
                    parms[5].Value = DateTime.Now;
                    
                    //rowsAffected = DBHelper.ExecuteNonQuery(cmdInsert, parms);

                    object o = DBHelper.ExecuteScalar(cmdInsert,parms);
                    if (o != null && o != DBNull.Value)
                    {
                        modelInfo.MyID = Convert.ToInt32(o);
                        result.Message = "会员数据保存成功！";
                        result.Result = true;
                        result.ReturnObject = modelInfo;
                    }



                }
                else // update
                {
                    //rowsAffected = UpdateByPrimaryKey(modelInfo, null);

                }
                //问题出在这里，判断条件错了,已更改为在插入操作时值变为1
                //if (rowsAffected > 0)
                //{
                //    result.Message = "会员数据保存成功！";
                //    result.Result = true;
                //    result.ReturnObject = modelInfo;
                //}
            }
            catch (Exception ex)
            {
                result.Exception = ex;
                result.Message = string.Format("保存会员数据时发生异常。异常信息：{0}", ex.Message);

            }

            return result;
        }
       public ResultInfo ADD(UserModel user)
       {
           if (user != null)
           {
               DeleteFN(user.MyUID);
               return Save(user);

           }
           else
           {
               throw new ArgumentNullException("modelInfo");
           }
          
       }
       private ResultInfo DeleteFN(string userUID)
       {
           ResultInfo result = new ResultInfo(false);
           try
           {
               string cmd = delbyUID;
               IDataParameter[] par = new SqlParameter[1];
               par[0] = new SqlParameter(C_COL_USER_UID,SqlDbType.NVarChar,50);
               par[0].Value = userUID;
               DBHelper.ExecuteNonQuery(cmd,par);
               result.Result = true;
               result.Message = "会员数据删除成功！";
           }
           catch (Exception ex)
           {
               result.Message = string.Format("删除会员数据时发生异常。异常信息：{0}", ex.Message);
           }

           return result;
       }
        
       
    }
}